package com.lyy;


import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.JdbcTypeMapping;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.cglib.util.StringSwitcher;


import javax.sql.DataSource;
import java.math.BigDecimal;
/**
 * @author Liangyaoyun
 */
@SuppressWarnings("all")
public class Codegen {
    /**
     * MySQL主机地址
     */
    private static final String MYSQL_HOST = "192.168.159.77:3306";
    /**
     * 数据库名
     */
    private static final String DB_NAME = "yunch_shop";
    /**
     * 数据库账号
     */
    private static final String MYSQL_USERNAME = "root";
    /**
     * 数据库密码
     */
    private static final String MYSQL_PASSWORD = "root";
    /**
     * 作者
     */
    private static final String AUTHOR = "LianYaoyun";
    /**
     * 子模块名: 指定生成到哪个子项目中
     */
    private static final String MODULE_NAME = "shop-user";
    /**
     * 根包
     */
    private static final String BASE_PACKAGE = "com.lyy";
    /**
     * 数据库表名前缀
     */
    private static final String[] TABLE_PREFIX = {"ums_", "pms_", "oms_", "cms_"};

    public static void main(String[] args) {
        // 将 BigDecimal 类型转为 Double 类型
        JdbcTypeMapping.registerMapping(BigDecimal.class, Double.class);
        new Generator(getDataSource(), getGlobalConfig()).generate();
    }

    /***
     * 获取MyBatisFlex生成器配置对象
     *
     * @return MyBatisFlex生成器配置对象
     */
    public static GlobalConfig getGlobalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setTablePrefix("cms_","oms_","pms_","sms_","ums_");

        // 注释配置: 作者
        globalConfig.getJavadocConfig()
                .setAuthor(AUTHOR);
        // 包配置: src目录，Mapper配置文件位置，根包
        globalConfig.getPackageConfig()
                .setSourceDir(MODULE_NAME + "/src/main/java")
                .setMapperXmlPath(MODULE_NAME + "/src/main/resources/mapper")
                .setBasePackage(BASE_PACKAGE);
        // 策略配置: 表前缀，数据库名，忽略哪些表
        // 可调用 setGenerateTable() 来指定生成哪些表: 未配置时，默认生成指定数据库下的所有表
        // 可调用 setUnGenerateTable() 来指定排除哪些表: 未配置时，不排除任何表
        globalConfig.getStrategyConfig()
                //.setTablePrefix(TABLE_PREFIX)
                .setGenerateSchema(DB_NAME)
                // .setGenerateTable("foreground")
                .setUnGenerateTable("cms_prefrence_area_product_relation", "cms_subject_product_relation",
                        "pms_product_category_attribute_relation",
                        "sms_coupon_product_category_relation",
                        "sms_coupon_product_relation",
                        "sms_flash_promotion_product_relation",
                        "sms_member_coupon_relation",
                        "ums_admin_permission_relation",
                        "ums_member_member_tag_relation",
                        "ums_member_product_category_relation",
                        "ums_role_menu_relation","ums_role_permission_relation",
                        "ums_role_resource_relation",
                        //cms
                        "cms_help",
                        "cms_help_category",
                        "cms_member_report",
                        "cms_prefrence_area",
                        "cms_prefrence_area_product_relation",
                        "cms_subject",
                        "cms_subject_category",
                        "cms_subject_comment",
                        "cms_subject_product_relation",
                        "cms_topic",
                        "cms_topic_category",
                        "cms_topic_comment",
                        //oms
                        "oms_cart_item",
                        "oms_company_address",
                        "oms_order",
                        "oms_order_item",
                        "oms_order_operate_history",
                        "oms_order_return_apply",
                        "oms_order_return_reason",
                        "oms_order_setting",
                //pms
                       "pms_album",
                       "pms_album_pic",
                       "pms_brand",
                       "pms_comment",
                       "pms_comment_replay",
                       "pms_feight_template",
                       "pms_member_price",
                       "pms_product",
                       "pms_product_attribute",
                       "pms_product_attribute_category",
                       "pms_product_attribute_value",
                       "pms_product_category",
                       "pms_product_category_attribute_relation",
                       "pms_product_full_reduction",
                       "pms_product_ladder",
                       "pms_product_operate_log",
                       "pms_product_vertify_record",
                       "pms_sku_stock",
                        //sms
                        "sms_coupon",
                        "sms_coupon_history",
                        "sms_coupon_product_category_relation",
                        "sms_coupon_product_relation",
                        "sms_flash_promotion",
                        "sms_flash_promotion_log",
                        "sms_flash_promotion_product_relation",
                        "sms_flash_promotion_session",
                        "sms_home_advertise",
                        "sms_home_brand",
                        "sms_home_new_product",
                        "sms_home_recommend_product",
                        "sms_home_recommend_subject",
                        "sms_member_coupon_relation"
                        //ums
//                        "ums_admin",
//                        "ums_admin_login_log",
//                        "ums_admin_permission_relation",
//                        "ums_admin_role_relation",
//                        "ums_growth_change_history",
//                        "ums_integration_change_history",
//                        "ums_integration_consume_setting",
//                        "ums_member",
//                        "ums_member_level",
//                        "ums_member_login_log",
//                        "ums_member_member_tag_relation",
//                        "ums_member_product_category_relation",
//                        "ums_member_receive_address",
//                        "ums_member_rule_setting",
//                        "ums_member_statistics_info",
//                        "ums_member_tag",
//                        "ums_member_task",
//                        "ums_menu",
//                        "ums_permission",
//                        "ums_resource",
//                        "ums_resource_category",
//                        "ums_role",
//                        "ums_role_menu_relation",
//                        "ums_role_permission_relation",
//                        "ums_role_resource_relation"
                );
        // 生成实体类，Mapper接口，Mapper配置，Service接口，Service实现类和Controller控制器
        globalConfig.enableEntity().setWithLombok(true);
        globalConfig.enableTableDef();
        globalConfig.enableMapper();

        globalConfig.enableService();
        globalConfig.enableServiceImpl().setCacheExample(true);
        globalConfig.enableController();
        // 返回MyBatisFlex生成器配置对象
        return globalConfig;
    }

    /***
     * 获取Hikari数据源对象
     *
     * @return Hikari数据源对象
     */
    public static DataSource getDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setUsername(MYSQL_USERNAME);
        dataSource.setPassword(MYSQL_PASSWORD);
        dataSource.setJdbcUrl("jdbc:mysql://" + MYSQL_HOST + "/" + DB_NAME
                + "?serverTimezone=Asia/Shanghai"
                + "&useUnicode=true&characterEncoding=utf-8");
        return dataSource;
    }
}
